﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Utilities;
using ViCode_LeVi.Data;
using CommandBinding;
using ViCode_LeVi.Configs;
using ViCode_LeVi.Reports;
namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabRiskManagement.xaml
    /// </summary>
    public partial class VMTabRiskManagement : UserControl
    {
        public VMTabRiskManagement()
        {
            InitializeComponent();
            InitializeComponent();
            new PropertyChangeNotifier(this, FrameworkElement.DataContextProperty, (s, e) =>
            {
                DisplayData();
            });
            this.Loaded += new RoutedEventHandler(VMTaskItemGroup_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(VMTaskItemGroup_SizeChanged);
            TabControl_Main.SelectionChanged += new SelectionChangedEventHandler(TabControl_Main_SelectionChanged);
        }

        void TabControl_Main_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TabControl_Main.SelectedItem == TabItem_Chart)
            {
                RiskChart.SetUpChart();
            }
        }

        void VMTaskItemGroup_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Setup_GridViewColumn_Define();
        }
        Step_RiskManagement _RiskManagement;
        void VMTaskItemGroup_Loaded(object sender, RoutedEventArgs e)
        {
            SetupDisplay();
            Setup_GridViewColumn_Define();
            SetupBindingCommand();
        }
        private void SetupDisplay()
        {
            Step_RiskManagement data = this.DataContext as Step_RiskManagement;
            if (data != null)
            {
                _RiskManagement = data;
                RiskChart.RiskManagementData = _RiskManagement;
            }
        }
        void Setup_GridViewColumn_Define()
        {
            GridView gv = ListView_TaskItems.View as GridView;
            if (gv != null && gv.Columns.Count > 0)
            {
                var sumW = gv.Columns.Where(c => c != GridViewColumn_Manage).Select(c => c.ActualWidth).Sum();
                var w = ListView_TaskItems.ActualWidth - sumW - 20;
                if (w > 0)
                    GridViewColumn_Manage.Width = w;
            }
        }
        private void SetupBindingCommand()
        {
            this.AssignCondition(Button_AddNew, (s, e) =>
            {
                Step_RiskManagement data = this.DataContext as Step_RiskManagement;
                if (data != null)
                {
                    long maxId = 0;
                    if (data.Tasks_Source.Count > 0)
                    {
                        maxId = data.Tasks_Source.Max(c => c.RiskNo);
                    }
                    data.Tasks_Source.Add(new Step_RiskManagementItem()
                    {
                        RiskNo = maxId + 1,
                        Risk = VMConfig.Intance.VMConfig_Step_RiskManagementItem_Default_Item.Risk,
                        Manage = VMConfig.Intance.VMConfig_Step_RiskManagementItem_Default_Item.Manage,
                        Impact = VMConfig.Intance.VMConfig_Step_RiskManagementItem_Default_Item.Impact,
                        Probability = VMConfig.Intance.VMConfig_Step_RiskManagementItem_Default_Item.Probability
                    });
                }
            }, (s, e) =>
            {
                e.OnCanExecute_SyncButtonVisibility(true, true);
            });
            this.AssignCondition(Button_Remove, (s, e) =>
            {
                if (MessageBox.Show("Do you want to delete this task?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    Step_RiskManagement data = this.DataContext as Step_RiskManagement;
                    if (data != null)
                    {
                        Step_RiskManagementItem deleteItem = ListView_TaskItems.SelectedItem as Step_RiskManagementItem;
                        if (deleteItem != null)
                        {
                            data.Tasks_Source.Remove(deleteItem);
                        }
                    }
                }
            }, (s, e) =>
            {
                e.OnCanExecute_SyncButtonVisibility(true, ListView_TaskItems.SelectedItem != null);

            });
        }

        private void DisplayData()
        {
            SetupDisplay();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_RiskManagement rep = new Report_RiskManagement();
            //var datasource = new List<Step_RiskManagementItem>();
            //datasource.Add(_RiskManagement.Tasks_Source);
            frmViewReport frm = new frmViewReport(rep, _RiskManagement.Tasks_Source);
            frm.Show();
        }
    }
}
